<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../static/js/vue.js"></script>
    <link rel="stylesheet" href="../../static/css/element-ui.css">

    <script src="../../static/js/element-ui.js"></script>
    <script src="../../static/js/axios.js"></script>

</head>
<body>
<div id="stock">
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="药名" prop="drugName">
                <el-input
                        v-model="queryParams.drugName"
                        placeholder="请输入药名"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item label="仓库" prop="warehouseId">
                <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库">

                    <el-option
                            v-for="item in warehouseList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"

                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                >新增
                </el-button>
            </el-col>



        </el-row>

        <el-table v-loading="loading" :data="stockList" >
            <el-table-column label="药名" align="center" prop="drugName"></el-table-column>
            <el-table-column label="剩余库存" align="center" prop="surplusStock"></el-table-column>
            <el-table-column label="仓库" align="center" >
                <template slot-scope="scope">
                    {{getWarehouse(scope.row.warehouseId)}}
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改
                    </el-button>

                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                style="float:right;margin-top: 20px"
                :page-sizes="[5, 10, 20, 50]"
                :page-size="5"
                v-show="total>0"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>

        <!-- 添加或修改库存对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="药名" prop="drugName">
                    <el-input v-model="form.drugName" placeholder="请输入药名"/>
                </el-form-item>
                <el-form-item label="库存" prop="surplusStock">
                    <el-input v-model="form.surplusStock" placeholder="请输入库存"/>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</div>
</body>


<script>

    new Vue({
        el: '#stock',
        data: function () {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                warehouseList:[],
                // 总条数
                total: 0,
                // 库存表格数据
                stockList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    drugName: null,
                    surplusStock: null,
                    warehouseId: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {}
            };
        },
        created() {
            this.getList();
            axios({
                url: '../../crud/warehouse/list',
                method: 'get',
                params: {}
            }).then(response => {
                console.log(response)
                this.warehouseList = response.data.rows;
                this.total = response.data.total;
                this.loading = false;
            });
        },
        methods: {
            /** 查询库存列表 */
            getList() {
                this.loading = true;
                axios({
                    url: '../../crud/stock/list',
                    method: 'get',
                    params: this.queryParams
                }).then(response => {
                    this.stockList = response.data.rows;
                    this.total = response.data.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    drugName: null,
                    surplusStock: null,
                    warehouseId: null
                };

            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.queryParams = {
                    pageNum: 1,
                    pageSize: 10,
                    drugName: null,
                    surplusStock: null,
                    warehouseId: null
                }
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加库存";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids;
                axios({
                    url: '../../crud/stock/' + id,
                    method: 'get'
                }).then(response => {
                    this.form = response.data.data;
                    this.open = true;
                    this.title = "修改库存";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            axios({
                                url: '../../crud/stock',
                                method: 'put',
                                data: this.form
                            }).then(response => {
                                if (response.data.code == 200) {
                                    this.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });

                                    this.getList();
                                } else {

                                    this.$message.error(response.data.msg);
                                }
                                this.open = false;
                            });
                        } else {
                            axios({
                                url: '../../crud/stock',
                                method: 'post',
                                data: this.form
                            }).then(response => {

                                if (response.data.code == 200) {
                                    this.$message({
                                        message: '新增成功',
                                        type: 'success'
                                    });
                                    this.open = false;
                                    this.getList();
                                } else {

                                    this.$message.error(response.data.msg);
                                }
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除库存编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delStock(ids);
                }).then((response) => {
                    this.getList();
                    if (response.data.code == 200) {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.open = false;
                        this.getList();
                    } else {

                        this.$message.error(response.data.msg);
                    }
                })
            },
            /** 导出按钮操作 */

            handleSizeChange(val) {
                this.queryParams.pageSize = val
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.pageNum = val;
                this.getList();
            },
            getWarehouse(str) {

                for (let i = 0; i < this.warehouseList.length; i++) {
                    if (str == this.warehouseList[i].id) {
                        return this.warehouseList[i].name
                    }
                }
            },
        }

    });
</script>
